package com.astersoft.lib.util;

/*
 * Copyright 2012 AsterSoft personal developer - Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * 
 * @author dario.durzo
 * @version 0.1
 * @created.date 2009.08.26*
 */
public class ASCompressionUtil {
	/**
	 * @param srcFolder path to the folder to be zipped
	 * @param destZipFile path to the final zip file
	 */
	public static boolean zipFolder(String srcFolder, String destZipFile) {
		if (new File(srcFolder).isDirectory()) {
			ZipOutputStream zip = null;
			FileOutputStream fileWriter = null;
			try {
				fileWriter = new FileOutputStream(destZipFile);
				zip = new ZipOutputStream(fileWriter);
			} catch (Exception ex) {
				ex.printStackTrace();
				return false;
			}

			addFolderToZip("", srcFolder, zip); //$NON-NLS-1$
			try {
				zip.flush();
				zip.close();
			} catch (Exception ex) {
				ex.printStackTrace();
				return false;
			}
			return true;
		} else {
			return false;
		}
	}



	/**
	 * @param zipFile the zip file that needs to be unzipped
	 * @param destFolder the folder into which unzip the zip file and create the folder structure
	 */
	public static void unzipFolder(String zipFile, String destFolder) {
		try {
			ZipFile zf = new ZipFile(zipFile);
			Enumeration< ? extends ZipEntry> zipEnum = zf.entries();
			String dir = destFolder;

			while( zipEnum.hasMoreElements() ) {
				ZipEntry item = (ZipEntry) zipEnum.nextElement();

				if (item.isDirectory()) {
					File newdir = new File(dir + File.separator + item.getName());
					newdir.mkdir();
				} else {
					String newfilePath = dir + File.separator + item.getName();
					File newFile = new File(newfilePath);
					if (!newFile.getParentFile().exists()) {
						newFile.getParentFile().mkdirs();
					}

					InputStream is = zf.getInputStream(item);
					FileOutputStream fos = new FileOutputStream(newfilePath);
					int ch;
					while( (ch = is.read()) != -1 ) {
						fos.write(ch);
					}
					is.close();
					fos.close();
				}
			}
			zf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	static private void addToZip( String path, String srcFile, ZipOutputStream zip ) {
		File folder = new File(srcFile);
		if (folder.isDirectory()) {
			addFolderToZip(path, srcFile, zip);
		} else {
			byte[] buf = new byte[1024];
			int len;
			FileInputStream in = null;
			try {
				in = new FileInputStream(srcFile);
				zip.putNextEntry(new ZipEntry(path + File.separator + folder.getName()));
				while( (len = in.read(buf)) > 0 ) {
					zip.write(buf, 0, len);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				if(in != null)
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
		}
	}


	static private void addFolderToZip( String path, String srcFolder, ZipOutputStream zip ) {
		File folder = new File(srcFolder);
		String listOfFiles[] = folder.list();
		try {
			for( int i = 0; i < listOfFiles.length; i++ ) {
				addToZip(path + File.separator + folder.getName(), srcFolder + File.separator
						+ listOfFiles[i], zip);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
